package com.itheima.Service.ServiceImpl;

import com.itheima.DTO.MemberDTO;
import com.itheima.Dao.MemberMapper;
import com.itheima.Service.MemberService;
import com.itheima.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
@Slf4j
public class MemberServiceImpl implements MemberService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private MemberMapper memberMapper;

    @Transactional
    public Boolean login(MemberDTO memberDTO){
        String trueCode = (String) redisTemplate.opsForHash().get("验证码", memberDTO.getTelephone());
        log.info("验证码：{}",trueCode);
        String validateCode = memberDTO.getValidateCode();
        if (!validateCode.equals(trueCode)){
            return false;
        }
        else {
            List<String> telephones = memberMapper.selectTelephone();
            if (telephones.contains(memberDTO.getTelephone())){
                return true;
            }
            else {
                LocalDate time = LocalDate.now();
                log.info("regTime：{}",time);
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                String regTime = time.format(formatter);
                memberMapper.insertMember(memberDTO,regTime);
                return true;
            }
        }


    }


}
